<?php
/**********************************************************
** Lime Manager
** Thomas Knobloch & Clément Troesch
** Manager Users Ajax View
***********************************************************/
?>
<?php include_once "../../model/SessionModel.php"; ?>
<?php session_start(); ?>
<?php include_once "../../model/ReferentialConnectionModel.php"; ?>
<?php include_once "../../model/UserModel.php"; ?>

<?php

//Suppression
if(isset($_GET['delete_user_id']))
{
	UserModel::deleteUser($_GET['delete_user_id']);
}


//Creation
if(isset($_GET['create_user_login']))
{
	UserModel::createUser($_GET['create_user_login'], $_GET['create_user_password'], $_GET['create_user_role']);
}

//Modification
if(isset($_GET['edit_user_id']))
{
	UserModel::editUser($_GET['edit_user_id'], $_GET['edit_user_login'], $_GET['edit_user_password'], $_GET['edit_user_role']);
}

?>

<div class="section_title" id="users"><h1> Utilisateurs </h1></div>
<div class="section_info">
	Gestion des utilisateurs.
</div>

<div class="article">

<table class="tableQueryCenter">
	<tr>
		<th>ID</th>
		<th>Login</th>
		<th>Mot de passe</th>
		<th>Rôle</th>
		<th>Actions</th>
	</tr>
<?php
	$nbrow = UserModel::getUsers($result);
	foreach ($result as $row) {
		echo "<tr>";
		echo "<td>".$row['ID']."</td>\n";
		echo "<td>".$row['LOGIN']."</td>\n";
		echo "<td>".$row['PASSWORD']."</td>\n";
		echo "<td>".$row['ROLE']."</td>\n";
		
		?>
		<td class="center">
			<a href="#utilisateur"><img alt="edit" src="img/edit.png" onClick="selectUser(<?php echo $row['ID'].", '".$row['LOGIN']."', '".$row['ROLE']."'" ?>)" /></a>
			
			<?php
			if($row['ID'] != $_SESSION['session']->userID)
			{
				?><a href="#users"><img alt="delete" src="img/delete.png" onClick="deleteUser(<?php echo $row['ID'] ?>)" /></a><?php
			}
			?>
		</td>
		<?php

		echo"</tr>";
	}
		
?>
</table>

<br /><br />

<?php /* Nouvel utilisateur */ ?>
<fieldset>
	<legend>Nouvel utilisateur</legend>
	<form method="post">
		<table>
		<tr>
			<!-- Login -->
			<td><label>Login :</label></td>
			<td>
				<input type="text" id="new_user_login" class="dbfield"/>
			</td>
		</tr>
		<tr>
			<!-- Password -->
			<td><label>Password :</label></td>
			<td>
				<input type="password" id="new_user_password" class="dbfield" />
			</td>
		</tr>
		<tr>
			<!-- Password -->
			<td><label>Confirmation :</label></td>
			<td>
				<input type="password" id="new_user_confirmation" class="dbfield" />
			</td>
		</tr>
		<tr>
			<!-- Role -->
			<td><label>Rôle :</label></td>
			<td>
				<select class="dbfield" id="new_user_role" >
					<option>GESTIONNAIRE</option>
					<option>ADMINISTRATEUR</option>
					<option>DEVELOPPEUR</option>
				</select>
			</td>
		</tr>
		<tr>
			<td></td>
			<td class="right"><input type="button" value="Envoyer" onClick="addUser()" /></td>
		</tr>
		</table>
		
		
	</form>
</fieldset>

<?php /* Modifier utilisateur */ ?>
<fieldset id="edit_user_field">
	<legend>Modifier un utilisateur</legend>
	<form method="post">
		<table>
		<tr>
			<!-- Login -->
			<td><label>ID :</label></td>
			<td>
				<span id="edit_user_id"></span>
			</td>
		</tr>
		<tr>
			<!-- Login -->
			<td><label>Login :</label></td>
			<td>
				<input type="text" id="edit_user_login" class="dbfield"/>
			</td>
		</tr>
		<tr>
			<!-- Password -->
			<td><label>Password :</label></td>
			<td>
				<input type="password" id="edit_user_password" class="dbfield" />
			</td>
		</tr>
		<tr>
			<!-- Password -->
			<td><label>Confirmation :</label></td>
			<td>
				<input type="password" id="edit_user_confirmation" class="dbfield" />
			</td>
		</tr>
		<tr>
			<!-- Role -->
			<td><label>Rôle :</label></td>
			<td>
				<select class="dbfield" id="edit_user_role" >
					<option>GESTIONNAIRE</option>
					<option>ADMINISTRATEUR</option>
					<option>DEVELOPPEUR</option>
				</select>
			</td>
		</tr>
		<tr>
			<td></td>
			<td class="right"><input type="button" value="Envoyer" onClick="editUser()" /></td>
		</tr>
		</table>
		
		
	</form>
</fieldset>

</div>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>  
<script type="text/javascript">
	$("#edit_user_field").attr('disabled', 'disabled');
</script>
